package com.frxuexi.visit.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.VisitBeanItem
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.visit.R
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.databinding.ActivityVisitApproveBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitApproveActivity")
@AndroidEntryPoint
class VisitApproveActivity : BaseActivity<ActivityVisitApproveBinding,VisitIntent,VisitViewModel>() {
    override val binding: ActivityVisitApproveBinding by lazy { ActivityVisitApproveBinding.inflate(layoutInflater) }
    override val vm: VisitViewModel by viewModels<VisitViewModel>()

    @JvmField
    @Autowired
    var id:Int?=-1

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.SELECT_ITEM->{
                                val item = it.data as VisitBeanItem
                                binding.bean=item
                                if(item.visitStatus=="待审核"){
                                    binding.visitDetailGroup.visibility=View.VISIBLE
                                }
                            }
                            RequestType.UPDATE->{
                                ToastUtils.showLong("审核完成")
                                finish()
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.sendIntent(VisitIntent.getVisitById(id!!))

        binding.visitDetailOk.setOnClickListener {
            vm.sendIntent(VisitIntent.approveVisit(id!!))
        }

        binding.visitDetailRefause.setOnClickListener {
            vm.sendIntent(VisitIntent.rejectVisit(id!!))
        }
    }

}